package com.fan.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fan.dao.PlayHistoryDao;
import com.fan.dao.VideoDao;
import com.fan.entity.PlayHistory;
import com.fan.entity.Video;
import com.fan.service.PlayHistoryService;
import com.sixth.until.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @program: self-tui-education
 * @description:
 * @author: 老范
 * @create: 2022-05-08 18:45
 **/
@Service
public class PlayHistoryServiceImpl extends ServiceImpl<PlayHistoryDao, PlayHistory> implements PlayHistoryService {
    @Autowired
    private PlayHistoryDao playHistoryDao;
    @Autowired
    private VideoDao videoDao;


    //    查询所有的播放信息
    @Override
    public CommonResult queryPlayHistory(Integer page, Integer limit, String HuiYuanId, PlayHistory playHistory) {
        Page<PlayHistory> historyPage = new Page<>(page,limit);
        QueryWrapper<PlayHistory> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",HuiYuanId);
        Page<PlayHistory> playHistoryPage = playHistoryDao.queryPlayHistory(historyPage, wrapper);
        return new CommonResult(2000, "查询成功", playHistoryPage);
    }



    //    根据课程id和会员id插入播放信息
    @Override
    public CommonResult insertPlayHistoryByCourseId(String vedeoId, String userId) {
        //        查询课程id查询课时信息(Video)
        Video video = videoDao.selectById(vedeoId);
//        根据课时信息表插入播放历史信息
//        播放id
        PlayHistory history = new PlayHistory();
        history.setVedeoId(vedeoId);
        //        设置主键
        String playId = UUID.randomUUID().toString().replace("-", "").substring(0, 19);
        history.setPlayId(playId);
        history.setUserId(userId);
//        播放时间
        history.setPlayTimePoint(new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss").format(new Date()));
        int insert = playHistoryDao.insert(history);
        return new CommonResult(2000, "查询成功", insert);
    }

    //  更新时间
    @Override
    public CommonResult updatePlayHistory(String userId, String vedeoId) {
        QueryWrapper<PlayHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("vedeo_id", vedeoId);
        PlayHistory playHistory = new PlayHistory();
        playHistory.setPlayTimePoint(new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss").format(new Date()));
        playHistoryDao.update(playHistory, queryWrapper);
        return new CommonResult(2000, "查询成功", queryWrapper);
    }
}
